import java.io.BufferedReader;   
import java.io.BufferedWriter;   
import java.io.FileReader;   
import java.io.FileWriter;   
import java.io.IOException;   
import java.util.ArrayList;   
import java.util.List;   
import java.util.Scanner;
   
class Team implements Comparable<Team> {
    String name;
    int played;
    int wins;
    int draws;
    int losses;
    int goalsFor;
    int goalsAgainst;
    int points;

    public Team(String name, int played, int wins, int draws, int losses, int goalsFor, int goalsAgainst, int points) {
        this.name = name;
        this.played = played;
        this.wins = wins;
        this.draws = draws;
        this.losses = losses;
        this.goalsFor = goalsFor;
        this.goalsAgainst = goalsAgainst;
        this.points = points;
    }

    public void update(int goalsFor, int goalsAgainst) {
        this.played++;
        this.goalsFor += goalsFor;
        this.goalsAgainst += goalsAgainst;
        if (goalsFor > goalsAgainst) {
            this.wins++;
            this.points += 3;
        } else if (goalsFor == goalsAgainst) {
            this.draws++;
            this.points += 1;
        } else {
            this.losses++;
        }
    }

    public int getGoalDifference() {
        return this.goalsFor - this.goalsAgainst;
    }

    @Override
    public int compareTo(Team other) {
        if (this.points!= other.points) {
            return other.points - this.points;
        } else if (this.getGoalDifference()!= other.getGoalDifference()) {
            return other.getGoalDifference() - this.getGoalDifference();
        } else if (this.goalsFor!= other.goalsFor) {
            return other.goalsFor - this.goalsFor;
        } else {
            return this.name.compareTo(other.name);
        }
    }

    @Override
    public String toString() {
        return String.format("%s: %d 分, 已赛 %d 场, 胜 %d, 平 %d, 负 %d, 进 %d, 失 %d, 净胜球 %d",
                name, points, played, wins, draws, losses, goalsFor, goalsAgainst, getGoalDifference());
    }   
}
   
public class Main {

    // 读取文件一行数据并转换为Team对象
    private static Team parseTeam(String line) {
        String[] data = line.split(",");
        String name = data[0].trim();
        int played = Integer.parseInt(data[1].trim());
        int wins = Integer.parseInt(data[2].trim());
        int draws = Integer.parseInt(data[3].trim());
        int losses = Integer.parseInt(data[4].trim());
        int goalsFor = Integer.parseInt(data[5].trim());
        int goalsAgainst = Integer.parseInt(data[6].trim());
        int points = Integer.parseInt(data[7].trim());
        return new Team(name, played, wins, draws, losses, goalsFor, goalsAgainst, points);
    }

    public static List<Team> readTable(String filename) throws IOException {
        List<Team> teams = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
            String line = br.readLine(); // 跳过标题行
            while ((line = br.readLine())!= null) {
                teams.add(parseTeam(line));
            }
        }
        return teams;
    }

    public static void writeTable(String filename, List<Team> teams) throws IOException {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) {
            bw.write("队名,已比赛场数,胜场数,平场数,负场数,进球数,失球数,积分");
            bw.newLine();
            for (Team team : teams) {
                bw.write(String.format("%s,%d,%d,%d,%d,%d,%d,%d",
                        team.name, team.played, team.wins, team.draws, team.losses,
                        team.goalsFor, team.goalsAgainst, team.points));
                bw.newLine();
            }
        }
    }

    public static Team findTeam(List<Team> teams, String teamName) {
        for (Team team : teams) {
            if (team.name.equals(teamName)) {
                return team;
            }
        }
        return null;
    }

    public static void updateTable(List<Team> teams, String homeTeam, String awayTeam, int homeGoals, int awayGoals) {
        Team home = findTeam(teams, homeTeam);
        Team away = findTeam(teams, awayTeam);
        if (home == null || away == null) {
            System.out.println("输入的队伍名称有误，请检查！");
            return;
        }
        home.update(homeGoals, awayGoals);
        away.update(awayGoals, homeGoals);
    }

    public static void main(String[] args) {
        String filename = "league_table.csv";
        try {
            List<Team> teams = readTable(filename);

            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入比赛结果（格式：主队 客队 主队得分 客队得分）：");
            String homeTeam = scanner.next();
            String awayTeam = scanner.next();
            int homeGoals = scanner.nextInt();
            int awayGoals = scanner.nextInt();

            updateTable(teams, homeTeam, awayTeam, homeGoals, awayGoals);

            teams.sort(Team::compareTo);
            writeTable(filename, teams);

            System.out.println("更新后的积分表：");
            for (Team team : teams) {
                System.out.println(team);
            }

        } catch (IOException e) {
            System.out.println("读取或写入文件时出错：" + e.getMessage());
        } catch (Exception e) {
            System.out.println("输入格式有误！");
        }
    }
}


